package nc.impl.pub.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.itf.pub.dbservice.IDBService;
import nc.jdbc.framework.generator.IdGenerator;
import nc.vo.pub.SuperVO;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFDateTime;
import nccloud.bs.log.LogVO;

/**
 * 操作数据库的工具
 * @author wy
 * @date 2023年4月10日
 */
public class DBServiceImpl implements IDBService{
	
	private final BaseDAO dao = new BaseDAO();

	@Override
	public LogVO buildLogVO(Map<String, Object> map, JSONObject json, String System_flag, String pk_billtypeid)
			throws Exception {
		LogVO logVO = new LogVO();
		logVO.setPk_id(NCLocator.getInstance().lookup(IdGenerator.class).generate());
		logVO.setPk_group(map.get("pk_group") == null ? "0001A210000000000JB9" : map.get("pk_group").toString());
		logVO.setPk_org(map.get("pk_org") == null ? "0001A210000000000JB9" : map.get("pk_org").toString() );
		logVO.setPk_org_v(map.get("pk_org_v") == null ? "0001A210000000000JB9" : map.get("pk_org_v").toString());
		logVO.setPk_billtypeid(pk_billtypeid);
		String vbillcode = "";
		if("voucher".equals(pk_billtypeid) ){
			vbillcode = String.format("%s_%s_%s_%s", map.get("agency_code"), map.get("set_year"), map.get("acct_period"), map.get("vbillcode")+"");
			logVO.setSobillno(map.get("vbillcode")+"");
		}else if("voucherDetail".equals(pk_billtypeid)){
			vbillcode = String.format("%s_%s_%s_%s", map.get("agency_code"), map.get("set_year"), map.get("acct_period"), map.get("vbillcode")+"");
			logVO.setSobillno(map.get("vbillcode")+"");
		}else if ("accountSubject".equals(pk_billtypeid)){
			vbillcode = map.get("kjkmjbxxcode")+"";
			logVO.setSobillno(vbillcode);
		}else{
			vbillcode = map.get("villcode")+"";
		}
		logVO.setVbillcode(vbillcode);
		logVO.setVdef1(json.toString());
		logVO.setSystem_flag(System_flag);
		logVO.setCreationtime(new UFDateTime());
		logVO.setDr("0");
		return logVO;
	}
	
	@Override
	public int updateSql(String sql) {
		try {
			return dao.executeUpdate(sql);
		} catch (Exception e) {
			nc.vo.pubapp.pattern.exception.ExceptionUtils.wrappException(e);
		}
		return 0;
	}
	
	@Override
	public int updateVO(SuperVO vo) {
		try {
			return dao.updateVO(vo);
		} catch (Exception e) {
			nc.vo.pubapp.pattern.exception.ExceptionUtils.wrappException(e);
		}
		return 0;
	}

	@Override
	public int updateVOArray(SuperVO[] vos) {
		try {
			return dao.updateVOArray(vos);
		} catch (Exception e) {
			nc.vo.pubapp.pattern.exception.ExceptionUtils.wrappException(e);
		}
		return 0;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void updateVOList(List listvo) {
		try {
			dao.updateVOList(listvo);
		} catch (Exception e) {
			nc.vo.pubapp.pattern.exception.ExceptionUtils.wrappException(e);
		}
	}

	@Override
	public String insertVO(SuperVO vo) {
		try {
			return dao.insertVO(vo);
		} catch (Exception e) {
			nc.vo.pubapp.pattern.exception.ExceptionUtils.wrappException(e);
		}
		return null;
	}

	@Override
	public String[] insertVOArray(SuperVO[] vos) {
		try {
			return dao.insertVOArray(vos);
		} catch (Exception e) {
			nc.vo.pubapp.pattern.exception.ExceptionUtils.wrappException(e);
		}
		return null;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public String[]  insertVOList(List listvo) {
		try {
			return dao.insertVOList(listvo);
		} catch (Exception e) {
			nc.vo.pubapp.pattern.exception.ExceptionUtils.wrappException(e);
		}
		return null;
	}

	@Override
	public String[] insertVOArray_RequiresNew(SuperVO[] vos) throws Exception {
		return dao.insertVOArray(vos);
	}

	@Override
	public int updateSql_RequiresNew(String sql) throws Exception {
		return dao.executeUpdate(sql);
	}
	
	@SuppressWarnings({ "rawtypes", "unused" })
	@Override
	public void updateVOList_RequiresNew(List listvo) throws Exception {
		if(listvo == null) return;
		
		List<SuperVO> add = new ArrayList<SuperVO>();
		List<SuperVO> up = new ArrayList<SuperVO>();
		for (int i = 0; i < listvo.size(); i++) {
			SuperVO vo = (SuperVO) listvo.get(i);
			if(vo.getStatus() == VOStatus.NEW){
				add.add(vo);
			}else if(vo.getStatus() == VOStatus.UPDATED || vo.getStatus() == VOStatus.DELETED){
				up.add(vo);
			}
		}
		if(add.size() > 0){
			try {
				dao.insertVOList(add);
				dao.executeUpdate("commit;");
			}catch(Exception e) {
				String error = e.getMessage(); 
			}
		}
		
		if(up.size() > 0){
			dao.updateVOList(listvo);
			dao.executeUpdate("commit;");
		}
	}

}
